package com.shujia.scala

class Person(id: String, name: String, age: Int) {
  val _id: String = id
  val _name: String = name
  val _age: Int = age

  def run() = {
    println("Person会run")
  }
}

// Scala中也是通过extends继承父类，在继承时就需要调用父类的构造方法
// 继承会默认获得父类的属性及方法
class Teacher(id: String, name: String, age: Int, skill: String) extends Person(id, name, age) {
  // 特有的属性
  val _skill: String = skill

  // 父类也有同名的方法，需要加上override重写
  override def run() = {
    println("Teacher会run")
  }

  def teach(): Unit = {
    println("老师会教课")
  }
}


class Student(id: String, name: String, age: Int, clazz: String) extends Person(id, name, age) {
  // 特有属性
  val _clazz: String = clazz

  // 父类也有同名的方法，需要加上override重写
  override def run() = {
    println("Student会run")
  }

  def student(): Unit = {
    println("学生会上课")
  }
}


object Demo14Extends {

  // 让Person对象run两次
  def personRunTwice(person:Person):Unit={
    person.run()
    person.run()
  }

//  // 让Teacher对象run两次
//  def teacherRunTwice(person:Teacher):Unit={
//    person.run()
//    person.run()
//  }
//
//  // 让Student对象run两次
//  def studentRunTwice(person:Student):Unit={
//    person.run()
//    person.run()
//  }

  def main(args: Array[String]): Unit = {
    val person:Person = new Person("001", "zs", 20)
    println(person._name)
    personRunTwice(person)

    val teacher:Teacher = new Teacher("001", "teach01", 30, "computer")
    println(teacher._name)
    println(teacher._skill)
    // 多态：父类引用指向子类对象
    personRunTwice(teacher)

    val student:Student = new Student("001", "student01", 20, "文科一班")
    println(student._name)
    println(student._clazz)
    personRunTwice(student)

  }
}
